package com.dindang.ddm.mvp.ui.adapter.custom;

import android.support.annotation.NonNull;
import android.support.constraint.ConstraintLayout;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.dindang.ddm.R;
import com.dindang.ddm.mvp.model.entity.Build;
import com.dindang.ddm.mvp.model.entity.Work;

import java.util.List;

/**首页工作模块按钮*/
public class WorkViewAdapter extends RecyclerView.Adapter<WorkViewAdapter.ViewHolder> {
    List<Work> list;
    OnClickListener listener;
    public WorkViewAdapter(List<Work> list) {
        this.list = list;
    }

    public WorkViewAdapter setListener(OnClickListener listener){
        this.listener = listener;
        return this;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_work, parent, false);
        WorkViewAdapter.ViewHolder viewHolder = new WorkViewAdapter.ViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Work work = list.get(position);
        holder.iv_image.setImageResource(work.getId());
        holder.tv_name.setText(work.getTitle());

        if(getItemCount() - 1 == position){
            holder.view_line.setVisibility(View.GONE);
        }else{
            holder.view_line.setVisibility(View.VISIBLE);
        }

        if(work.getId() != 0){
            holder.constraintlayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    listener.onClick(position);
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return list.size();
    }


    class ViewHolder extends RecyclerView.ViewHolder {
        ConstraintLayout constraintlayout;
        ImageView iv_image;
        TextView tv_name;
        View view_line;
        ViewHolder(View itemView) {
            super(itemView);
            constraintlayout = itemView.findViewById(R.id.constraintlayout);
            iv_image = itemView.findViewById(R.id.iv_image);
            tv_name = itemView.findViewById(R.id.tv_name);
            view_line = itemView.findViewById(R.id.view_line);
        }
    }

    public interface OnClickListener {
        void onClick(int position);
    }

}